<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TaskService
 *
 * @author admin
 */
class Service_TaskService {
    public function __construct()
    {

    }
    public function getTask($id)
    {
        $taskMapper = new Application_Model_TaskMapper();
        $task = $taskMapper->fetchById($id);

        $taskVO = $this->_toTaskVO($task);

        return $taskVO;
    }

    /**
     * @return array
     */
    public function getTasks()
    {   
        $taskMapper = new Application_Model_TaskMapper();
        $tasks = $taskMapper->fetchAll();

        $taskVOs = $this->_toTaskVOs($tasks);

        return $taskVOs;
    }
    
    /**
     * @return array
     */
    public function getTasksFromProjectId($projectId)
    {
        $taskMapper = new Application_Model_TaskMapper();
        $tasks = $taskMapper->fetchByProjectId($projectId);

        $taskVOs = $this->_toTaskVOs($tasks);

        return $taskVOs;
    }

    /**
     * @return array
     */
    public function getTasksList()
    {
        return $this->getTasks();
    }
    
    public function addTask($content, $starttime, $endtime, $projectId)
    {
        $taskMapper = new Application_Model_TaskMapper();
        if($taskMapper->addTask($content, $starttime, $endtime, $projectId))
        {
            return true;
        }
        else{
            return false;
        }
    }
    
    public function updateTask($id, $content, $starttime, $endtime){
        $taskMapper = new Application_Model_TaskMapper();
        if($taskMapper->updateTask($id, $content, $starttime, $endtime))
        {
            return true;
        }
        else{
            return false;
        }
    }
    
    public function deleteTask($id){
        $taskMapper = new Application_Model_TaskMapper();
        if($taskMapper->deleteTask($id))
        {
            return true;
        }
        else{
            return false;
        }
    }

    /**
     * @param Application_Model_Task $task
     * @return VO_Task
     */
    private function _toTaskVO($task)
    {
        
        $taskVO = new VO_Task();
        $taskVO->id             = $task->Id;
        $taskVO->content        = $task->Content;
        $taskVO->starttime      = $task->Starttime;
        $taskVO->endtime        = $task->Endtime;
        $taskVO->projectId      = $task->ProjectId;

        return $taskVO;
    }

    /**
     * @param array $tasks
     * @return array
     */
    private function _toTaskVOs($tasks = array())
    {
        $taskVOs = array();
        foreach ($tasks as $task) {
            $taskVOs[] = $this->_toTaskVO($task);
        }

        return $taskVOs;
    }
    
    public function logOut()
    {
        Zend_Auth::getInstance()->clearIdentity();
    }
}

?>
